package cn.itcast.scm.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.scm.entity.SysParam;
import cn.itcast.scm.service.SysParamService;
import cn.itcast.scm.service.impl.SysParamServiceImpl;

public class SysInitServlet extends HttpServlet{
	
	private SysParamService sysParamService = new SysParamServiceImpl();
	
	public void destroy() {
		super.destroy();
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
		Map<String,Object> sysParam = new HashMap<String,Object>();
		Map<String,String> supType = new HashMap<String,String>();
    /*	supType.put("1", "普通供应商");
    	supType.put("2", "一级供应商");
    	supType.put("3", "二级供应商");*/
    	//1. 处理供应商类型
		//1.1  从数据库查出供应商类型
    	/*String sysParamField = "supType";*/
    	List<SysParam> supTypes = sysParamService.selectByParamField("supType");
    	//1.2  把供应商类型放到map
    	for(SysParam sup : supTypes) {
    		supType.put(sup.getSysParamText(), sup.getSysParamValue());
    	}
    	//1.3  把map放到sysParam中
    	sysParam.put("supType", supType);
    	
    	
    	Map<String,String> goodsColor = new HashMap<String,String>();
    	//1. 处理商品颜色
    	/*goodsColor.put("red", "红色");
    	goodsColor.put("green", "绿色");
    	goodsColor.put("blue", "蓝色");*/
    	List<SysParam> goodsColors = sysParamService.selectByParamField("goodsColor");
    	for(SysParam g : goodsColors){
    		goodsColor.put(g.getSysParamText(), g.getSysParamValue());
    	}
    	sysParam.put("goodsColor", goodsColor);
    	
    	ServletContext application =this.getServletContext();
    	application.setAttribute("sysParam",sysParam);
		System.out.println("=======初始化完成，已加载数据字典数据=======");
	}
}
